import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/20 10:14
 * @description 前缀和 + 排序
 */
public class Solution {
    public static void main(String[] args) {

    }
    public int rangeSum(int[] nums, int n, int left, int right) {
        int length = nums.length;
        int[] preSum = new int[length + 1];
        for (int i = 0; i < length; i++) {
            preSum[i + 1] = preSum[i] + nums[i];
        }
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            for (int j = i; j < length; j++) {
                list.add(preSum[j + 1] - preSum[i]);
            }
        }
        Collections.sort(list);
        int ans = 0;
        int mod = (int) 1e9 + 7;
        for (int i = left; i <= right; i++) {
            ans += list.get(i-1);
            ans %= mod;
        }
        return ans;
    }
}
